package com.hyd.admin.resource.domain;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class Resource {
	private BigDecimal resourceId;
	private String resourceName;
	private String resourcePath;
	private boolean asMenu;
	private BigDecimal parentResourceId;
	private int serialNumber;
	private String createdBy;
	private Date createdDate;

	public static final BigDecimal firstLevelResourceParentId = BigDecimal.ZERO;
	private List<Resource> childResourceList;

	public BigDecimal getResourceId() {
		return resourceId;
	}

	public void setResourceId(BigDecimal resourceId) {
		this.resourceId = resourceId;
	}

	public String getResourcePath() {
		return resourcePath;
	}

	public void setResourcePath(String resourcePath) {
		this.resourcePath = resourcePath;
	}

	public boolean isAsMenu() {
		return asMenu;
	}

	public void setAsMenu(boolean asMenu) {
		this.asMenu = asMenu;
	}

	public BigDecimal getParentResourceId() {
		return parentResourceId;
	}

	public void setParentResourceId(BigDecimal parentResourceId) {
		this.parentResourceId = parentResourceId;
	}

	public String getCreatedBy() {
		return createdBy;
	}

	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public List<Resource> getChildResourceList() {
		return childResourceList;
	}

	public void setChildResourceList(List<Resource> childResourceList) {
		this.childResourceList = childResourceList;
	}

	public int getSerialNumber() {
		return serialNumber;
	}

	public void setSerialNumber(int serialNumber) {
		this.serialNumber = serialNumber;
	}

	public static List<Resource> buildResourceTree(List<Resource> resourceList) {
		List<Resource> result = null;
		if (resourceList != null) {
			result = orderAndLinkResource(resourceList, null);
		}
		return result;
	}

	private static List<Resource> orderAndLinkResource(List<Resource> list,
			Resource parent) {
		BigDecimal parentResourceId = firstLevelResourceParentId;
		if (parent != null) {
			parentResourceId = parent.getResourceId();
		}
		List<Resource> children = new ArrayList<Resource>();
		for (Resource ri : list) {
			if (ri.getParentResourceId().longValue() == parentResourceId
					.longValue()) {
				children.add(ri);
			}
		}
		list.removeAll(children);
		Collections.sort(children, new ResourceComparator());
		if (parent != null && children.size() > 0) {
			parent.setChildResourceList(children);
		}
		for (Resource ri : children) {
			orderAndLinkResource(list, ri);
		}
		return children;
	}

	public static class ResourceComparator implements Comparator<Resource> {
		@Override
		public int compare(Resource o1, Resource o2) {
			if (o1 != null && o2 != null) {
				return o1.getSerialNumber() - o2.getSerialNumber();
			}
			return 0;
		}
	}

	public static void main(String[] args) {
		List<Resource> children = new ArrayList<Resource>();

		Resource r1 = new Resource();
		r1.setResourceId(new BigDecimal(1));
		r1.setSerialNumber(1);
		r1.setParentResourceId(null);

		Resource r2 = new Resource();
		r2.setResourceId(new BigDecimal(2));
		r2.setSerialNumber(2);
		r2.setParentResourceId(null);

		Resource r3 = new Resource();
		r3.setResourceId(new BigDecimal(3));
		r3.setSerialNumber(3);
		r3.setParentResourceId(new BigDecimal(1));

		Resource r4 = new Resource();
		r4.setResourceId(new BigDecimal(4));
		r4.setSerialNumber(4);
		r4.setParentResourceId(new BigDecimal(1));
		children.add(r4);
		children.add(r2);
		children.add(r3);
		children.add(r1);
		System.out.println(children);
		System.out.println(buildResourceTree(children));
	}

	public String getResourceName() {
		return resourceName;
	}

	public void setResourceName(String resourceName) {
		this.resourceName = resourceName;
	}
}
